#!/usr/bin/env python

## gtk imports
import pygtk
pygtk.require('2.0')

import gtk
from gtk import gdk

import cairo

if gtk.pygtk_version < (2,7,99):
    print "PyGtk 2.7.99 or later required"
    raise SystemExit

import time

data = [(u'2007-01-19 00:38', 4.9100000000000001),
        (u'2007-01-19 01:00', 4.9100000000000001),
        (u'2007-01-19 01:30', 4.9100000000000001),
        (u'2007-01-19 02:00', 4.9100000000000001),
        (u'2007-01-19 02:30', 4.9100000000000001),
        (u'2007-01-19 03:00', 4.9100000000000001),
        (u'2007-01-19 03:30', 4.9100000000000001),
        (u'2007-01-19 04:00', 4.9100000000000001),
        (u'2007-01-19 04:30', 4.9100000000000001),
        (u'2007-01-19 05:00', 4.9199999999999999),
        (u'2007-01-19 05:30', 4.9199999999999999),
        (u'2007-01-19 06:00', 4.9199999999999999),
        (u'2007-01-19 06:30', 4.9199999999999999),
        (u'2007-01-19 07:00', 4.9199999999999999),
        (u'2007-01-19 07:30', 4.9199999999999999),
        (u'2007-01-19 08:00', 4.9199999999999999),
        (u'2007-01-19 08:30', 4.9199999999999999),
        (u'2007-01-19 09:00', 4.9199999999999999),
        (u'2007-01-19 09:30', 4.9199999999999999),
        (u'2007-01-19 10:00', 4.9199999999999999),
        (u'2007-01-19 10:30', 4.9199999999999999),
        (u'2007-01-19 11:00', 4.9199999999999999),
        (u'2007-01-19 11:30', 4.9199999999999999),
        (u'2007-01-19 12:00', 4.9199999999999999),
        (u'2007-01-19 12:30', 4.9199999999999999),
        (u'2007-01-19 13:00', 4.9199999999999999),
        (u'2007-01-19 13:30', 4.9199999999999999),
        (u'2007-01-19 14:00', 4.9100000000000001),
        (u'2007-01-19 14:30', 4.9000000000000004),
        (u'2007-01-19 15:00', 4.9000000000000004),
        (u'2007-01-19 15:30', 4.9000000000000004),
        (u'2007-01-19 16:00', 4.9000000000000004),
        (u'2007-01-19 16:30', 4.8899999999999997),
        (u'2007-01-19 17:00', 4.8899999999999997),
        (u'2007-01-19 17:30', 4.8899999999999997),
        (u'2007-01-19 18:00', 4.8899999999999997),
        (u'2007-01-19 18:30', 4.8899999999999997),
        (u'2007-01-19 19:00', 4.8899999999999997),
        (u'2007-01-19 19:30', 4.8899999999999997),
        (u'2007-01-19 20:00', 4.8899999999999997),
        (u'2007-01-19 20:30', 4.8899999999999997),
        (u'2007-01-19 21:00', 4.8899999999999997),
        (u'2007-01-19 21:30', 4.8899999999999997),
        (u'2007-01-19 22:00', 4.8899999999999997),
        (u'2007-01-19 22:30', 4.8899999999999997),
        (u'2007-01-19 23:00', 4.8799999999999999),
        (u'2007-01-19 23:30', 4.8799999999999999),
        (u'2007-01-20 00:00', 4.8799999999999999),
        (u'2007-01-20 00:30', 4.6399999999999997),
        (u'2007-01-20 01:00', 4.6299999999999999),
        (u'2007-01-20 01:30', 4.6299999999999999),
        (u'2007-01-20 02:00', 4.6299999999999999),
        (u'2007-01-20 02:30', 4.71),
        (u'2007-01-20 03:00', 4.71),
        (u'2007-01-20 03:30', 4.71),
        (u'2007-01-20 04:00', 4.8499999999999996),
        (u'2007-01-20 04:30', 4.8499999999999996),
        (u'2007-01-20 05:00', 4.8499999999999996),
        (u'2007-01-20 05:30', 4.8499999999999996),
        (u'2007-01-20 06:00', 4.8499999999999996),
        (u'2007-01-20 06:30', 4.8499999999999996),
        (u'2007-01-20 07:00', 4.8499999999999996),
        (u'2007-01-20 07:30', 4.8499999999999996),
        (u'2007-01-20 08:00', 4.8499999999999996),
        (u'2007-01-20 08:30', 4.8499999999999996),
        (u'2007-01-20 09:00', 4.8499999999999996),
        (u'2007-01-20 09:30', 4.8499999999999996),
        (u'2007-01-20 10:00', 4.8499999999999996),
        (u'2007-01-20 10:30', 4.8499999999999996),
        (u'2007-01-20 11:00', 4.8499999999999996),
        (u'2007-01-20 11:30', 4.8499999999999996),
        (u'2007-01-20 12:00', 4.8499999999999996),
        (u'2007-01-20 12:30', 4.8499999999999996),
        (u'2007-01-20 13:00', 4.8499999999999996),
        (u'2007-01-20 13:30', 4.8499999999999996),
        (u'2007-01-20 14:00', 4.8499999999999996),
        (u'2007-01-20 14:30', 4.8499999999999996),
        (u'2007-01-20 15:00', 4.8499999999999996),
        (u'2007-01-20 15:30', 4.8499999999999996),
        (u'2007-01-20 16:00', 4.8499999999999996),
        (u'2007-01-20 16:30', 4.8499999999999996),
        (u'2007-01-20 17:00', 4.8499999999999996),
        (u'2007-01-20 17:30', 4.8499999999999996),
        (u'2007-01-20 18:00', 4.8399999999999999),
        (u'2007-01-20 18:30', 4.8399999999999999),
        (u'2007-01-20 19:00', 4.8399999999999999),
        (u'2007-01-20 19:30', 4.8399999999999999),
        (u'2007-01-20 20:00', 4.8300000000000001),
        (u'2007-01-20 20:30', 4.8300000000000001),
        (u'2007-01-20 21:00', 4.8300000000000001),
        (u'2007-01-20 21:30', 4.8300000000000001),
        (u'2007-01-20 22:00', 4.8300000000000001),
        (u'2007-01-20 22:30', 4.8300000000000001),
        (u'2007-01-20 23:00', 4.8300000000000001),
        (u'2007-01-20 23:30', 4.8300000000000001),
        (u'2007-01-21 00:00', 4.8300000000000001),
        (u'2007-01-21 00:30', 4.8300000000000001),
        (u'2007-01-21 01:00', 4.8300000000000001),
        (u'2007-01-21 01:30', 4.9000000000000004),
        (u'2007-01-21 02:00', 4.9000000000000004),
        (u'2007-01-21 02:30', 4.9000000000000004),
        (u'2007-01-21 03:00', 4.9000000000000004),
        (u'2007-01-21 03:30', 4.9000000000000004),
        (u'2007-01-21 04:00', 4.9000000000000004),
        (u'2007-01-21 04:30', 4.8899999999999997),
        (u'2007-01-21 05:00', 4.8899999999999997),
        (u'2007-01-21 05:30', 4.8899999999999997),
        (u'2007-01-21 06:00', 4.8899999999999997),
        (u'2007-01-21 06:30', 4.8899999999999997),
        (u'2007-01-21 07:00', 4.9000000000000004),
        (u'2007-01-21 07:30', 4.9000000000000004),
        (u'2007-01-21 08:00', 4.9000000000000004),
        (u'2007-01-21 08:30', 4.8899999999999997),
        (u'2007-01-21 09:00', 4.8200000000000003),
        (u'2007-01-21 09:30', 4.8200000000000003),
        (u'2007-01-21 10:00', 4.8200000000000003),
        (u'2007-01-21 10:30', 4.8200000000000003),
        (u'2007-01-21 11:00', 4.8200000000000003),
        (u'2007-01-21 11:30', 4.8200000000000003),
        (u'2007-01-21 12:00', 4.8200000000000003),
        (u'2007-01-21 12:30', 4.8200000000000003),
        (u'2007-01-21 13:00', 4.8200000000000003),
        (u'2007-01-21 13:30', 4.8200000000000003),
        (u'2007-01-21 14:00', 4.8200000000000003),
        (u'2007-01-21 14:30', 4.8200000000000003),
        (u'2007-01-21 15:00', 4.9299999999999997),
        (u'2007-01-21 15:30', 4.9299999999999997),
        (u'2007-01-21 16:00', 4.9199999999999999),
        (u'2007-01-21 16:30', 4.8600000000000003),
        (u'2007-01-21 17:00', 4.8600000000000003),
        (u'2007-01-21 17:30', 4.8499999999999996),
        (u'2007-01-21 18:00', 4.8499999999999996),
        (u'2007-01-21 18:30', 4.8499999999999996),
        (u'2007-01-21 19:00', 4.8499999999999996),
        (u'2007-01-21 19:30', 4.8200000000000003),
        (u'2007-01-21 20:00', 4.8200000000000003),
        (u'2007-01-21 20:30', 4.8200000000000003),
        (u'2007-01-21 21:00', 4.8099999999999996),
        (u'2007-01-21 21:30', 4.8099999999999996),
        (u'2007-01-21 22:00', 4.8099999999999996),
        (u'2007-01-21 22:30', 4.7199999999999998),
        (u'2007-01-21 23:00', 4.7199999999999998),
        (u'2007-01-21 23:30', 4.7199999999999998),
        (u'2007-01-22 00:00', 4.7199999999999998),
        (u'2007-01-22 00:30', 4.7199999999999998),
        (u'2007-01-22 01:00', 4.71),
        (u'2007-01-22 01:30', 4.8899999999999997),
        (u'2007-01-22 02:00', 4.8899999999999997),
        (u'2007-01-22 02:30', 4.8700000000000001),
        (u'2007-01-22 03:00', 4.8700000000000001),
        (u'2007-01-22 03:30', 4.8600000000000003),
        (u'2007-01-22 04:00', 4.8499999999999996),
        (u'2007-01-22 04:30', 4.8399999999999999),
        (u'2007-01-22 05:00', 4.8300000000000001),
        (u'2007-01-22 05:30', 4.8200000000000003),
        (u'2007-01-22 06:00', 4.8200000000000003),
        (u'2007-01-22 06:30', 4.8200000000000003),
        (u'2007-01-22 07:00', 4.8200000000000003),
        (u'2007-01-22 07:30', 4.8200000000000003),
        (u'2007-01-22 08:00', 4.8200000000000003),
        (u'2007-01-22 08:30', 4.8200000000000003),
        (u'2007-01-22 09:00', 4.8200000000000003),
        (u'2007-01-22 09:30', 4.8200000000000003),
        (u'2007-01-22 10:00', 4.8200000000000003),
        (u'2007-01-22 10:30', 4.8200000000000003),
        (u'2007-01-22 11:00', 4.8200000000000003),
        (u'2007-01-22 11:30', 4.8200000000000003),
        (u'2007-01-22 12:00', 4.8099999999999996),
        (u'2007-01-22 12:30', 4.8099999999999996),
        (u'2007-01-22 13:00', 4.8099999999999996),
        (u'2007-01-22 13:30', 4.8099999999999996),
        (u'2007-01-22 14:00', 4.8099999999999996),
        (u'2007-01-22 14:30', 4.8099999999999996),
        (u'2007-01-22 15:00', 4.8200000000000003),
        (u'2007-01-22 15:30', 4.8200000000000003),
        (u'2007-01-22 16:00', 4.8099999999999996),
        (u'2007-01-22 16:30', 4.8099999999999996),
        (u'2007-01-22 17:00', 4.8099999999999996),
        (u'2007-01-22 17:30', 4.8099999999999996),
        (u'2007-01-22 18:00', 4.8099999999999996),
        (u'2007-01-22 18:30', 4.8099999999999996),
        (u'2007-01-22 19:00', 4.7999999999999998),
        (u'2007-01-22 19:30', 4.7999999999999998),
        (u'2007-01-22 20:00', 4.7999999999999998),
        (u'2007-01-22 20:30', 4.7999999999999998),
        (u'2007-01-22 21:00', 4.7999999999999998),
        (u'2007-01-22 21:30', 4.7999999999999998),
        (u'2007-01-22 22:00', 4.7999999999999998),
        (u'2007-01-22 22:30', 4.7999999999999998),
        (u'2007-01-22 23:00', 4.7999999999999998),
        (u'2007-01-22 23:30', 5.1900000000000004),
        (u'2007-01-23 00:00', 5.1900000000000004),
        (u'2007-01-23 00:30', 5.0700000000000003),
        (u'2007-01-23 01:00', 4.8700000000000001),
        (u'2007-01-23 01:30', 5.0599999999999996),
        (u'2007-01-23 02:00', 5.1200000000000001),
        (u'2007-01-23 02:30', 5.2300000000000004),
        (u'2007-01-23 03:00', 5.2300000000000004),
        (u'2007-01-23 03:30', 5.2300000000000004),
        (u'2007-01-23 04:00', 5.2300000000000004),
        (u'2007-01-23 04:30', 5.2300000000000004),
        (u'2007-01-23 05:00', 4.9500000000000002),
        (u'2007-01-23 05:30', 4.9500000000000002),
        (u'2007-01-23 06:00', 4.9500000000000002),
        (u'2007-01-23 06:30', 4.9500000000000002),
        (u'2007-01-23 07:00', 4.9900000000000002),
        (u'2007-01-23 07:30', 4.9900000000000002),
        (u'2007-01-23 08:00', 4.9900000000000002),
        (u'2007-01-23 08:30', 4.9900000000000002),
        (u'2007-01-23 09:00', 4.9900000000000002)]

class HistoryWidget(gtk.DrawingArea):
    def __init__(self, data):
        gtk.DrawingArea.__init__(self)
        self.red=100
        self.data= [(self.get_timestamp(x[0]),x[1]) for x in data]
        self.begin = self.data[0][0]
        self.end = self.data[-1][0]

        print self.begin, self.end
        
        self.add_events(gtk.gdk.BUTTON_PRESS_MASK |
                        gtk.gdk.BUTTON_RELEASE_MASK |
                        gtk.gdk.BUTTON1_MOTION_MASK |
                        gtk.gdk.SCROLL_MASK
                        )

        self.connect("button_press_event", self.on_button_press_event)
        self.connect("expose_event", self.do_expose_event)

    def get_timestamp(self, time_str):
        return time.mktime(time.strptime(time_str,'%Y-%m-%d %H:%M'))

    def translate_x_coord(self, time_coord, time_begin, time_end):
        if time_coord >= time_begin and time_coord <= time_end:
            return (time_coord - time_begin) / (time_end - time_begin)
        return None
        
    def do_expose_event(self,w, event):
        try:
            self.context = self.window.cairo_create()
        except AttributeError:
            #return self._expose_gdk(event)
            raise
        return self._expose_cairo(event)

    def _expose_cairo(self, event):
        self.context.rectangle(event.area.x, event.area.y,
                     event.area.width, event.area.height)
        self.context.clip()	
        self.draw()

    def refresh(self):
        print "do_refresh"
        x, y, w, h = self.allocation
        self.window.invalidate_rect((0,0,w,h),False)

    def draw(self):
        print "draw"
        # background
        x, y, w, h = self.allocation
        try:
            self.context.rectangle (0,0,w,h)
            self.context.set_source_rgb(self.red, 0, 0) 
            self.context.fill ()

            self.context.rectangle (0,0,w,h)
            self.context.set_line_width(1)
            self.context.set_source_rgb(0, 0, 0) 
            self.context.stroke()

            self.context.set_source_rgb(0, 0, 0)

            first = True
            for dato in self.data:
                if self.translate_x_coord(dato[0],self.begin, self.end):
                    self.context.set_source_rgb(0, 0, 0)
                    import random
                    y_aleat = random.randint(0, 100) / 100.0
                    
                    myx= int(self.translate_x_coord(dato[0],self.begin, self.end) * w)
                    myy = (dato[1]/25.0*-h )+ h                

                    if not first:
                        self.context.line_to(myx, myy)
                        print myx, myy
                        self.context.set_line_width(1)
                        self.context.stroke()
                    else:
                        first = False
                    self.context.move_to(myx, myy)

            
        except cairo.Error, e:
            # FIXME: says "cairo.Error: the target surface has been finished"
            print "!", e
            pass

    def on_button_press_event(self, w,e):
        self.begin += 3600 * 4
        self.refresh()


h=HistoryWidget(data)

window = gtk.Window(gtk.WINDOW_TOPLEVEL)

window.add(h)
window.show_all()
window.connect("delete_event",gtk.main_quit)
window.connect("destroy",gtk.main_quit)

gtk.main()
